package com.ls.atf.core.web.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import com.ls.atf.core.web.WebPage;
import com.ls.atf.core.web.elements.CustomElement;
import com.ls.atf.data.UserData;

public class HomePage extends WebPage<HomePage> {

	private static final String USERNAME_XPATH_LOCATOR = "//a[@class='drop']/child::span[@class='username-wrap']/small[@class='username']";

	public HomePage(WebDriver driver) {
		super(driver);
	}

	@Override
	public HomePage load() {
		return this;
	}

	@Override
	public boolean isAvailable() {
		return getUserDisplayName().isAvailable();
	}

	public boolean isLoggedIn(UserData user) {
		return isContentPresent(
				driver.findElement(By.xpath(USERNAME_XPATH_LOCATOR)),
				user.getDisplayName());
	}

	private boolean isContentPresent(WebElement element, String text) {
		return element.getText().equals(text);
	}

	private CustomElement getUserDisplayName() {
		return new CustomElement(driver, By.xpath(USERNAME_XPATH_LOCATOR));
	}

}
